package com.sixmac.common.controller;

import com.sixmac.common.base.BaseController;
import com.sixmac.core.constant.ConstConfig;
import com.sixmac.utils.FileUploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import java.util.ArrayList;
import java.util.List;


/**
 * create by blade.
 * 2017-10-17 15:07:56
 */
@Controller
@RequestMapping("/common")
public class CommonController extends BaseController {

	@ResponseBody
	@RequestMapping("/upload/addTempImage")
	public String addTempImage(MultipartRequest multipartRequest) {
		String path = null;
		try {
			MultipartFile multipartFile = multipartRequest.getFile("tempImage");

			// 验证图片格式
			String originalFileName = multipartFile.getOriginalFilename().toLowerCase();
			String fileType = originalFileName.substring(originalFileName.lastIndexOf("."));

			List<String> list = new ArrayList<>();
			list.add(".jpg");
			list.add(".gif");
			list.add(".jpeg");
			list.add(".png");
			list.add(".bmp");

			if (!list.contains(fileType)) {
				return "";
			}
			path = ConstConfig.UPLOAD_PATH + FileUploadUtil.uploadFile(multipartFile);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return path;
	}
	@ResponseBody
	@RequestMapping("/upload/addFile")
	public String addFile(MultipartRequest multipartRequest) {
		String path = null;
		try {
			MultipartFile multipartFile = multipartRequest.getFile("tempFile");

			// 验证文件格式
			String originalFileName = multipartFile.getOriginalFilename().toLowerCase();
			String fileType = originalFileName.substring(originalFileName.indexOf("."));

			List<String> list = new ArrayList<>();
			list.add(".pdf");
			list.add(".doc");

			if (!list.contains(fileType)) {
				return "";
			}
			path = ConstConfig.UPLOAD_PATH + FileUploadUtil.uploadFile(multipartFile);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return path;
	}
}
